<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="slider">
import { BasicTable, TableAction, ActionItem } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/Drawer';
import Drawer from './components/drawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

import { columns, searchFormSchema } from './data/slider.data';
import { Api } from './data/slider.api';
const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'slider-template',
  tableProps: {
    //表格标题
    title: '轮播图列表',
    //主键字段
    rowKey: 'sliderId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //默认排序(排序字段上需设置defaultSortOrder:'descend')
    defSort: {
      order: 'createTime',
      sort: 'desc',
    },
    //操作列配置
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      icon: 'clarity:note-edit-line',
      onClick: handleEdit.bind(null, record),
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        icon: 'ant-design:delete-outlined',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '发布',
      popConfirm: {
        title: '确认发布吗?',
        confirm: handleSetState.bind(null, record),
      },
      ifShow: (_action) => {
        return record.sendStatus == 0;
      },
    },
    {
      label: '下架',
      popConfirm: {
        title: '确认下架吗?',
        confirm: handleSetState.bind(null, record),
      },
      ifShow: (_action) => {
        return record.sendStatus == 1;
      },
    },
  ];
}

/**
 * 更新发布状态
 */
async function handleSetState(record) {
  await handlePost(Api.setSendStatus, {
    ids: record.sliderId,
    sendStatus: record.sendStatus == 1 ? 0 : 1,
  });
}
function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}
</script>
